import org.junit.Test;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;




public class HybridFW {
	//public int xRow; int xCol;
	public int iWait;
	public String dURL,	dilogin,	dipswd,	dqinput, dExpText;
	long timeTaken;  // time to measure the test step taken.
	Date timeStart, timeEnd;
	
	
	
	
	@Test
	public void qS1() throws Exception{
		
		
		String[][] xTC, xTS, xTD;
		String vKW, vXpath, vTestData;
		FirefoxDriver myD = new FirefoxDriver();
		dURL = "https://qa.silverbackmdm.com/admin";
		dilogin = "Priyap";
		dipswd ="S1lverb@ck";
		
		

	
		
		String xPath = "C:/Priya/Silverback 4.3/Automation/QServicehf.xls";
		
		//Read the entire TC and TS
		xTC = XL1.xlRead(xPath,"TestCases");
		xTS = XL1.xlRead(xPath, "TestSteps");
		xTD = XL1.xlRead(xPath, "TestData");
		
		System.out.println("Test Step sheet is" + xTS);
		
		//Go to each row in TC and identify which TC to execute
		
		for (int k = 1; k<xTD.length; k++){ //Go to each row of test data and read the values of each variable
		
			if (xTD[k][6].equalsIgnoreCase("Y")){  //Is the TD for executiin
				
				//Read the values of the variables from test Data SHeet				
							
							for (int i = 1; i<xTC.length; i++) {   //Go to each row in TC								
						
								System.out.println("TCID  is " + xTC[i][0]);
								
								if (xTC[i][3].equalsIgnoreCase("Y")) {
									xTC[i][4] = "Pass"; //default TC result is a pass
									
									System.out.println("Yes, execute this");
									timeStart = new Date(); //Returns seconds from some specific ate in 1970's
								
								//go to every test step if it is y
								for (int j=1; j<xTS.length; j++){
									Thread.sleep(iWait);
									System.out.println("TSID is" +xTS[i][0]);
									Thread.sleep(iWait);
									if (xTC[i][0].equalsIgnoreCase(xTS[j][1])){
										//Thread.sleep(iWait);
										System.out.println("KW to execute is " + xTS[j][4]);
										Thread.sleep(iWait);
										System.out.println("xpath to execute is " + xTS[j][5]);
										System.out.println("Data to execute is " + xTS[j][6]);
										
										vKW = xTS[j][4] ;
										vXpath = xTS[j][5];
										vTestData = xTS[j][6];
										String vError = "Pass";
										// call the corresponding KW function
										
										//Get the corresponding Test Data value based on the name of the TD variable in TS
										vTestData = getTDValue(vTestData); //
										try{
											
											if (vKW.equalsIgnoreCase("enterText")) {
												KW.enterText(myD, vXpath, vTestData);
											}
												if (vKW.equalsIgnoreCase("navigateBrowser")){
												KW.navigateBrowser(myD, vTestData);
												}
													if(vKW.equalsIgnoreCase("clickElement")){
													KW.clickElement(myD, vXpath);
													}
														if (vKW.equalsIgnoreCase("verifyText")){
															vError = KW.verifyText(myD, vXpath, vTestData);
															System.out.println("Test is a" + vError );
														}
														
														if (vKW.equalsIgnoreCase("readText")){
															vError = KW.readText(myD, vXpath);
															System.out.println("Test is a" + vError); // These methods return something and they are only print. If this line fails, it still going as a pass.They will not throw exceptions
															// 
														}
														if (vKW.equalsIgnoreCase("waitTime")){
															KW.waitTime(3000);
														}
														if (vError.equalsIgnoreCase("Pass")){
														
																System.out.println("Pass" + xTC[i][0] + "_" + vKW);
																xTS[j][8] = "Pass";
											
														} else {
															System.out.println("Fail" + xTC[i][0] + "_" + vKW);
															xTS[j][8] = "Fail";
															xTS[j][9] = "Error happened as function returned Fail" ;
															xTC[i][4] = "Fail";
															
														}
										} catch (Exception myError){ //If an error occurred then the code will go to the catch exception. Anything to do with Application will get captured here.
											System.out.println("Fail" + xTC[i][0] + "_" + vKW);   
											xTS[j][8] = "Fail";
											//System.out.println("Error happened" + myError);
											xTS[j][9] = "Error happened" + myError;
											xTC[i][4] = "Fail";
											
										}
										
													
									}
								}
								}
							}
							
							//Write back the test steps to Excel
							
							XL1.writeXL("C:\\Priya\\Silverback 4.3\\Results\\KDF-TCResults-"+xTD[k][0]+".xls", "TestCases Results", xTC);
							XL1.writeXL("C:\\Priya\\Silverback 4.3\\Results\\KDF-TSResults-"+xTD[k][0]+".xls", "TestSteps Results", xTS);
						}
	}
		myD.quit();
	}


	private String getTDValue(String vTestData) {
	
		// TODO Auto-generated method stub
		
		if (vTestData.equals("dURL")){
			return dURL;   //returns the value in dURL
		}
		
		if (vTestData.equals("dilogin")){
			return dilogin;   //returns the value in dURL
		}
		if (vTestData.equals("dipswd")){
			return dipswd;   //returns the value in dURL
		}
		if (vTestData.equals("dExpText")){
			return dExpText;   //returns the value in dURL
			
			
		}
		

		

	
	//return "No test data found ";  //either return the values set above,or return the values as is such as the expected results or 
     return "vTestData"; //returns the vTest Data as is such as the verifications. Taking vtest data as is...
	}
	
public void executeKW(String vKW, String vXP, String vTD){
	
}
}
		
		
						
						

		
	

			
		
		